package iamutkarshtiwari.github.io.ananas.editimage.utils;


import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;

import java.math.BigDecimal;

public class RectUtil {

    public static void scaleRect(RectFloat rect, float scale) {
        RectUtil.scaleRect(rect, scale, scale);
    }

    public static void scaleRect(RectFloat rect, double scaleX, double scaleY) {
        if (scaleX == 1 && scaleY == 1) return;
        double width = rect.getWidth();
        double height = rect.getHeight();
        double newW = scaleX * width;
        double newH = scaleY * height;
        double dx = BigDecimal.valueOf(newW).subtract(BigDecimal.valueOf(width)).doubleValue() / 2D;
        double dy = BigDecimal.valueOf(newH).subtract(BigDecimal.valueOf(height)).doubleValue() / 2D;
        rect.left = (float) ((double) rect.left - dx);
        rect.top = (float) ((double) rect.top - dy);
        rect.right = (float) ((double) rect.right + dx);
        rect.bottom = (float) ((double) rect.bottom + dy);
    }

    public static void translateRect(RectFloat rect, double tarnX, double tranY) {
        rect.left = (float) ((double) rect.left + tarnX);
        rect.top = (float) ((double) rect.top + tranY);
        rect.right = (float) ((double) rect.right + tarnX);
        rect.bottom = (float) ((double) rect.bottom + tranY);
    }

    /**
     * 旋转角度
     *
     * @param rect        原图的浮点矩阵
     * @param center_x    x轴中心点
     * @param center_y    y轴中心点
     * @param roatetAngle 选择角度
     */
    public static void rotateRect(RectFloat rect, double center_x, double center_y, double roatetAngle) {
        float pointX = rect.getCenter().getPointX();
        float pointY = rect.getCenter().getPointY();

        BigDecimal sinA = BigDecimal.valueOf(Math.sin(Math.toRadians(roatetAngle)));
        BigDecimal cosA = BigDecimal.valueOf(Math.cos(Math.toRadians(roatetAngle)));
        BigDecimal subtractX = BigDecimal.valueOf(pointX).subtract(BigDecimal.valueOf(center_x));
        BigDecimal subtractY = BigDecimal.valueOf(pointY).subtract(BigDecimal.valueOf(center_y));

        float newX = (float) (center_x + subtractX.multiply(cosA).floatValue() - subtractY.multiply(sinA).floatValue());
        float newY = (float) (center_y + subtractY.multiply(cosA).floatValue() + subtractX.multiply(sinA).floatValue());

        rect.translateCenterTo(newX, newY);
    }

    public static void rectAddV(final RectFloat srcRect, final RectFloat addRect, int padding) {
        if (srcRect == null || addRect == null)
            return;

        float left = srcRect.left;
        float top = srcRect.top;
        float right = srcRect.right;
        float bottom = srcRect.bottom;

//        if (srcRect.width() <= addRect.width()) {
//            right = left + addRect.width();
//        }
//        bottom += padding + addRect.height();
//        srcRect.set(left, top, right, bottom);
//        if (srcRect.getWidth() <= addRect.getWidth()) {
//            right = left + addRect.getWidth();
//        }
//
//        bottom += padding + addRect.getHeight();

        srcRect.modify(left, top, right, bottom);
    }

    public static void rectAddV(final Rect srcRect, final Rect addRect, int padding, int charMinHeight) {
        if (srcRect == null || addRect == null)
            return;

        int left = srcRect.left;
        int top = srcRect.top;
        int right = srcRect.right;
        int bottom = srcRect.bottom;

//        if (srcRect.width() <= addRect.width()) {
//            right = left + addRect.width();
//        }
//        bottom += padding + Math.max(addRect.height(), charMinHeight);
//        if (srcRect.getWidth() <= addRect.getWidth()) {
//            right = left + addRect.getWidth();
//        }
//
//        bottom += padding + Math.max(addRect.getHeight(), charMinHeight);

        srcRect.set(left, top, right, bottom);
    }
}
